package com.shop.cereshop.app.param.wallet;

import com.shop.cereshop.commons.config.RsaProperties;
import com.shop.cereshop.commons.utils.RsaUtils;
import com.shop.cereshop.commons.utils.EmptyUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.validation.constraints.NotBlank;

/**
 * 设置支付密码请求参数
 * 只包含设置密码需要的字段
 */
@Data
@ApiModel(value = "SetPayPasswordRequest", description = "设置支付密码请求参数")
public class SetPayPasswordRequest {
    
    @ApiModelProperty(value = "加密后的支付密码", required = true, example = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...", notes = "使用RSA公钥加密的6位数字支付密码")
    @NotBlank(message = "支付密码不能为空")
    private String encryptedPayPassword;
    
    @ApiModelProperty(value = "加密后的确认支付密码", required = true, example = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...", notes = "使用RSA公钥加密的6位数字确认密码")
    @NotBlank(message = "确认支付密码不能为空")
    private String encryptedConfirmPayPassword;
    
    /**
     * 解密支付密码
     */
    @JsonIgnore
    public String decryptPayPassword() {
        if (EmptyUtils.isEmpty(encryptedPayPassword)) {
            return null;
        }
        try {
            return RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, encryptedPayPassword);
        } catch (Exception e) {
            throw new RuntimeException("支付密码解密失败", e);
        }
    }
    
    /**
     * 解密确认支付密码
     */
    @JsonIgnore
    public String decryptConfirmPayPassword() {
        if (EmptyUtils.isEmpty(encryptedConfirmPayPassword)) {
            return null;
        }
        try {
            return RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, encryptedConfirmPayPassword);
        } catch (Exception e) {
            throw new RuntimeException("确认支付密码解密失败", e);
        }
    }
    
    /**
     * 验证支付密码格式
     */
    @JsonIgnore
    public boolean isPayPasswordValidFormat() {
        String password = decryptPayPassword();
        return password != null && password.matches("^\\d{6}$");
    }
    
    /**
     * 验证确认支付密码格式
     */
    @JsonIgnore
    public boolean isConfirmPayPasswordValidFormat() {
        String password = decryptConfirmPayPassword();
        return password != null && password.matches("^\\d{6}$");
    }
    
    /**
     * 验证两次密码是否一致
     */
    @JsonIgnore
    public boolean isPasswordMatch() {
        String password = decryptPayPassword();
        String confirmPassword = decryptConfirmPayPassword();
        return password != null && confirmPassword != null && password.equals(confirmPassword);
    }

    /**
     *解密支付密码
     */
    @JsonIgnore
    public String decryptPayPasswordParam(String encryptedPayPassword) {
        if (EmptyUtils.isEmpty(encryptedPayPassword)) {
            return null;
        }
        try {
            return RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, encryptedPayPassword);
        } catch (Exception e) {
            throw new RuntimeException("支付密码解密失败", e);
        }
    }

    public static void main(String[] args) {
        SetPayPasswordRequest request = new SetPayPasswordRequest();
        request.decryptPayPasswordParam("$2a$10$S6fTxCdPvRC3amplBXeCAuf2K4fGWRn1tsziCRc8TUqgvz6BiOf1m");
    }
}
